package com.budwk.app.sys.commons.task.job;

import cn.hutool.core.lang.TypeReference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.dto.TransferServiceRecordDTO;
import com.budwk.app.sqfw.models.Sqfw_servicerecord;
import com.budwk.app.sys.models.Sys_task;
import com.budwk.app.sys.services.SysTaskService;
import com.budwk.starter.job.annotation.SJob;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.integration.jedis.pubsub.PubSubService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wizzer@qq.com
 */
@IocBean(name = "taskJob",singleton = false)
@Slf4j
public class TaskJob implements Job{
    @Inject
    PubSubService pubSubService;

    @Inject
    SysTaskService sysTaskService;

    ExecutorService executorService= Executors.newFixedThreadPool(20);

   /* @Inject
    SqfwServicerecordService sqfwServicerecordService;

    @Inject
    SqfwWorkorderService sqfwWorkorderService;

    @Inject
    SysUserService sysUserService;

    @Inject
    HuaweiOssService huaweiOssService;

    @Inject
    SqfwCopyrecordService sqfwCopyrecordService;*/




    @SJob("createTaskJob")
    public void createTaskJob(String taskId, String params) throws JsonProcessingException {

        Cnd cnd=Cnd.NEW();
        cnd.and("disabled","=",false);
        cnd.and("flag","=","create");
        try {
            Map<String, String> map = JSON.parseObject(params, new TypeReference<Map<String, String>>() {});
            ObjectMapper objectMapper = new ObjectMapper();

            String TransferServiceRecordDTOString = objectMapper.writeValueAsString(map.get("transferServiceRecordDTO"));
            String sqfw_servicerecordString = objectMapper.writeValueAsString(map.get("sqfwservicerecord"));
            String imgIdString = objectMapper.writeValueAsString(map.get("imgId"));
            // 请求参数比DTO属性多，对map进行配置忽略多余参数检查  add by LJT 2024-04-15
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            TransferServiceRecordDTO transferServiceRecordDTO=null;
            String ImgId ="";
            if (StringUtils.isNotBlank(TransferServiceRecordDTOString)){
                transferServiceRecordDTO = objectMapper.readValue(TransferServiceRecordDTOString, TransferServiceRecordDTO.class);
            }
            if (StringUtils.isNotBlank(imgIdString)){
                ImgId = objectMapper.readValue(imgIdString, String.class);
            }
            Sqfw_servicerecord sqfw_servicerecord = objectMapper.readValue(sqfw_servicerecordString, Sqfw_servicerecord.class);
            sysTaskService.quartzCreateServiceRecord(sqfw_servicerecord,transferServiceRecordDTO,ImgId);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Cnd cnd=Cnd.NEW();
        cnd.and("disabled","=",false);
        cnd.and("flag","=","creat");
        executorService.execute(()->{
            try {
                Map<String, String> map = JSON.parseObject(context.getJobDetail().getJobDataMap().get("params").toString(), new TypeReference<Map<String, String>>() {});
                ObjectMapper objectMapper = new ObjectMapper();

                String TransferServiceRecordDTOString = objectMapper.writeValueAsString(map.get("transferServiceRecordDTO"));
                String sqfw_servicerecordString = objectMapper.writeValueAsString(map.get("sqfwservicerecord"));
                String imgIdString = objectMapper.writeValueAsString(map.get("imgId"));
                // 请求参数比DTO属性多，对map进行配置忽略多余参数检查  add by LJT 2024-04-15
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                TransferServiceRecordDTO transferServiceRecordDTO=null;
                String ImgId ="";
                if (StringUtils.isNotBlank(TransferServiceRecordDTOString)){
                    transferServiceRecordDTO = objectMapper.readValue(TransferServiceRecordDTOString, TransferServiceRecordDTO.class);
                }
                if (StringUtils.isNotBlank(imgIdString)){
                    ImgId = objectMapper.readValue(imgIdString, String.class);
                }
                Sqfw_servicerecord sqfw_servicerecord = objectMapper.readValue(sqfw_servicerecordString, Sqfw_servicerecord.class);

                    sysTaskService.quartzCreateServiceRecord(sqfw_servicerecord,transferServiceRecordDTO,ImgId);


            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }


}
